﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;

public static class IEnumeratorExtension
{
	public static T Random<T>(this IEnumerable<T> source, Func<T,string> predicate, float maxValue = 100f)
    {
        T result = default(T);
        float culativeSum = 0, randomValue = UnityEngine.Random.Range(1f, maxValue);
        foreach (T item in source)
        {
            result = item;
            //Debug.Log(" Item : " + predicate(item));
            culativeSum += float.Parse(predicate(item));
            if (randomValue < culativeSum)
                break;
        }
        return result;
    }
}
